package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"
}

func (c *MainController) Test() {
	c.Data["Website"] = "beego1.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"
}

func (c *MainController) GetFunc() {
	//获取请求数据,get/post都可以
	a:=c.GetString("a")
	fmt.Println(a)

	fmt.Println("main get func")

	c.Ctx.WriteString(a)

	//获取请求数据,get/post都可以
	b:=c.Input().Get("a")
	fmt.Println("b=",b)

	d:=c.Input().Get("d")
	fmt.Println("d=",d)

	e,_:=c.GetInt("e")
	fmt.Println("e=",e)
}

func (c *MainController) PostFunc() {
	fmt.Println("main post func")
	c.Data["json"] = MyStruct{Id: 1, Name: "zs"}

	a := MyStruct{Id: 2, Name: "ls"}
	fmt.Println(a.getName())

	c.ServeJSON()
}

func (c *MainController) RegApi() {
	id:=c.Ctx.Input.Param(":id")
	fmt.Println(id)
	fmt.Println(c.XSRFToken())
}

type MyStruct struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

func (m *MyStruct) getName() string {
	return m.Name
}
